﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Eneta.Portal.Common.Repository;
using Microsoft.SharePoint;
using Eneta.Portal.Common.Helpers;

namespace Eneta.Portal.Literature.Controls
{
    public class RandomLiteratureControl : UserControl
    {
        protected HtmlAnchor randomLiteratureTitleLink;
        protected HtmlAnchor randomLiteratureImageLink;
        protected HtmlImage randomLiteratureImage;
        protected HtmlAnchor randomLiteratureItemTitleLink;
        protected HtmlGenericControl randomLiteratureCommentsCount;
        protected HtmlGenericControl randomLiteratureDescription;
        protected HtmlGenericControl randomLiteratureImageContainer;
        protected HtmlGenericControl randomLiteratureTextContainer;

        public string WebUrl { get; set; }
        public string ContentTypeName { get; set; }
        public string Title { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            this.randomLiteratureTitleLink.HRef = WebUrl;
            this.randomLiteratureTitleLink.InnerText = Title;

            BindRandomItem();
        }

        private void BindRandomItem()
        {
            var item = new LiteratureRepository(SPContext.Current.Web).GetRandomLiteratureItem(WebUrl, ContentTypeName);
            if (item == null)
            {
                this.randomLiteratureImageContainer.Visible = false;
                this.randomLiteratureTextContainer.Visible = false;
                return;
            }

            this.randomLiteratureItemTitleLink.HRef = item.ItemUrl;
            randomLiteratureItemTitleLink.Title = item.Title;
            this.randomLiteratureItemTitleLink.InnerHtml = item.Title;

            //TODO: LiteratureItems comments count missing
            this.randomLiteratureCommentsCount.InnerText = "";

            this.randomLiteratureDescription.InnerHtml = RegExHelper.RemoveHtmlTags(item.ShortSummary) + " ...";

            this.randomLiteratureImageLink.HRef = item.ItemUrl;
            randomLiteratureImageLink.Title = item.Title;
            this.randomLiteratureImage.Src = item.LiteratureSmallImageUrl;

        }

    }
}
